package util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SendMail {

//    public static void main(String args[]) {
//        try {
//            //n6-069-060cl.privatedns.
//            send("mail.emailexclusivo.com.br", "phat10130059@gmail.com",
//                    "codigo@emailexclusivo.com.br", "Mn@3101!@#$", "thử phát", "<h1>Đây chính là cái mail</h1><br />"
//                    + "<a href=\"http://localhost:8080/DemoSendMailWeb/ActiveAction?username=phat&code=123\">kich hoat</a>");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
    public static void send(String smtpServer, String to, String from,
            String psw, String subject, String body) throws Exception {




        // Code provide by congdongjava.com
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "mail.emailexclusivo.com.br");
        props.put("mail.smtp.port", "25");
        props.put("mail.smtp.starttls.enable", "false");

        final String login = from;
        final String pwd = psw;
        Authenticator pa = null;
        if (login != null && pwd != null) {
            props.put("mail.smtp.auth", "true");
            pa = new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(login, pwd);
                }
            };
        }// else: no authentication
        Session session = Session.getInstance(props, pa);
        // — Create a new message –
        Message msg = new MimeMessage(session);
        // — Set the FROM and TO fields –
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to, false));

        // — Set the subject and body text –
        msg.setSubject(subject);
        //msg.setText(body);// Để gởi nội dung dạng utf-8 các bạn dùng
        msg.setContent(body, "text/html; charset=UTF-8");
        // — Set some other header information –
        msg.setHeader("X-Mailer", "LOTONtechEmail");
        msg.setSentDate(new Date());
        msg.saveChanges();
        // — Send the message –
        Transport.send(msg);
        System.out.println("Mail da duoc gui");


    }

    public static void gmailSend( String to, String from,
            String psw, String subject, String body) throws Exception {
        // Code provide by congdongjava.com
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.starttls.enable", "true");
        final String login = from;
        final String pwd = psw;
        Authenticator pa = null;
        if (login != null && pwd != null) {
            props.put("mail.smtp.auth", "true");
            pa = new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(login, pwd);
                }
            };
        }// else: no authentication
        Session session = Session.getInstance(props, pa);
        // — Create a new message –
        Message msg = new MimeMessage(session);
        // — Set the FROM and TO fields –
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to, false));

        // — Set the subject and body text –
        msg.setSubject(subject);
        //msg.setText(body);// Để gởi nội dung dạng utf-8 các bạn dùng
        msg.setContent(body, "text/html; charset=UTF-8");
        // — Set some other header information –
        msg.setHeader("X-Mailer", "LOTONtechEmail");
        msg.setSentDate(new Date());
        msg.saveChanges();
        // — Send the message –
        Transport.send(msg);
        System.out.println("Mail da duoc gui");


    }
}
